/**
 * Title: MethodUtils.java<br/>
 * Description: <br/>
 * Copyright: Copyright (c) 2015<br/>
 * Company: gigold<br/>
 */
package com.hktf.uilib.utils;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Title: MethodUtils<br/>
 * Description: <br/>
 * Company: gigold<br/>
 *
 * @author xie_lp
 * @date 2015-10-16下午2:45:04 方法集合类
 */
public class MethodUtils {
    private static MethodUtils instance = null;

    private MethodUtils() {
        super();
    }

    public static MethodUtils getInstance() {
        if (null == instance) {
            instance = new MethodUtils();
        }
        return instance;
    }

    /**
     * Title: judgeEditText<br/>
     * Description: <br/>
     *
     * @param editText 字符串
     * @return
     * @author xie_lp
     * @date 2015-10-16下午2:52:09 判断字符串是否为空，如果为空，返回true否则返回false
     */
    public boolean judgeStrIsEmpty(String editText) {
        if ("".equals(editText) || null == editText) {
            return true;
        }
        return false;
    }








    /**
     * 金额转换 保留小点后两位数字 Title: getMoneyAsStr<br/>
     * Description: <br/>
     *
     * @param type 2：保留两位，4：保留两位
     * @return
     * @author xie_lp
     * @date 2015-12-2上午9:25:49
     */
    public String convertMoneyAsStr(double amt, int type) {
        DecimalFormat fnum = null;
        if (type == 2) {
            fnum = new DecimalFormat("##0.00");
        } else if (type == 3) {
            fnum = new DecimalFormat("##0.000");
        } else if (type == 4) {
            fnum = new DecimalFormat("##0.0000");
        } else {
            fnum = new DecimalFormat("##0.00");
        }
        String ret = fnum.format(amt);
        return ret;
    }

    public float converMoney(double amt) {
        long l1 = Math.round(amt * 1000); // 四舍五入
        double ret = l1 / 1000.0; // 注意：使用 100.0 而不是 100
        return (float) ret;
    }

    /**
     * dip换算成px Title: dip2px<br/>
     * Description: <br/>
     *
     * @param context
     * @param dipValue
     * @return
     * @author xie_lp
     * @date 2015-11-10上午11:54:54
     */
    public int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * px换算成dip Title: px2dip<br/>
     * Description: <br/>
     *
     * @param context
     * @param pxValue
     * @return
     * @author xie_lp
     * @date 2015-11-10上午11:55:19
     */
    public int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将px值转换为sp值 保证文字大小不变 Title: px2sp<br/>
     * Description: <br/>
     *
     * @param context
     * @param pxValue
     * @return
     * @author xie_lp
     * @date 2015-11-17上午10:43:25
     */
    public int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px值 保证文字大小不变 Title: sp2px<br/>
     * Description: <br/>
     *
     * @param context
     * @param spValue
     * @return
     * @author xie_lp
     * @date 2015-11-17上午10:43:33
     */
    public int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().density;
        return (int) (spValue * fontScale + 0.5f);
    }









    /*
     * 判断是否为整数
     * @param str 传入的字符串
     * @return 是整数返回true,否则返回false
     */
    public boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    /**
     * @param borderColor
     */
    public static ShapeDrawable createRoundCornerShapeDrawable(Context context, String borderColor) {
        float[] outerRadii = new float[]{MethodUtils.getInstance().dip2px(context, 3), MethodUtils.getInstance().dip2px(context, 3), MethodUtils.getInstance().dip2px(context, 3), MethodUtils.getInstance().dip2px(context, 3), 0, 0, 0, 0};

        ShapeDrawable sd = new ShapeDrawable(new RoundRectShape(outerRadii, null,
                null));
        sd.getPaint().setColor(Color.parseColor(borderColor));
        sd.getPaint().setStyle(Paint.Style.FILL);
        return sd;
    }

    public boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    //判断，返回布尔值
    public boolean isPhoneNumber(String input) {
        String regexStr = "1([\\d]{10})|((\\+[0-9]{2,4})?\\(?[0-9]+\\)?-?)?[0-9]{7,8}";
        Pattern p = Pattern.compile(regexStr);
        return p.matcher(input).matches();
    }

    /**
     * 从字符串中获取电话号码
     */
    public String getNumbers(String content) {
//        StringBuilder mobile = new StringBuilder();
        String mobile = null;
//        String regexStr = "1([\\d]{10})|((\\+[0-9]{2,4})?\\(?[0-9]+\\)?-?)?[0-9]{7,8}";
        String regexStr = "1[3|4|5|7|8|][0-9]{9}";
        Pattern p = Pattern.compile(regexStr);
        Matcher m = p.matcher(content);
        while (m.find()) {
            String find = m.group().toString();
//            mobile.append(find);
            mobile = find;
        }
        return mobile;
    }

    /**
     * Title:
     * Description:SD卡是否存在
     *
     * @param
     * @author xie_lp
     * @data 16/6/28 上午2:36
     */
    public boolean ExistSDCard() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            return true;
        } else
            return false;
    }

    /**
     * Title:
     * Description:SD卡剩余空间
     *
     * @param
     * @author xie_lp
     * @data 16/6/28 上午2:36
     */
    public long getSDFreeSize() {
        //取得SD卡文件路径  
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());
        //获取单个数据块的大小(Byte)  
        long blockSize = sf.getBlockSize();
        //空闲的数据块的数量  
        long freeBlocks = sf.getAvailableBlocks();
        //返回SD卡空闲大小  
        //return freeBlocks * blockSize;  //单位Byte  
        //return (freeBlocks * blockSize)/1024;   //单位KB  
        return (freeBlocks * blockSize) / 1024 / 1024; //单位MB
    }
}
